package com.example.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.news.mapper.ArticleMapper;
import com.example.news.mapper.UserMapper;
import com.example.news.pojo.entity.SearchRecords;
import com.example.news.pojo.entity.User;
import com.example.news.pojo.form.BasePageQuery;
import com.example.news.pojo.vo.SearchArticleVo;
import com.example.news.pojo.vo.SearchRecordVo;
import com.example.news.service.SearchService;
import com.example.news.mapper.SearchRecordsMapper;
import com.example.news.utils.JwtHelper;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.ResultCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86147
 * @description 针对表【search_records】的数据库操作Service实现
 * @createDate 2023-10-23 09:33:10
 */
@Service
public class SearchServiceImpl extends ServiceImpl<SearchRecordsMapper, SearchRecords>
        implements SearchService {

    @Autowired
    private SearchRecordsMapper searchRecordsMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageResult<SearchRecordVo> getSearchRecords(BasePageQuery basePageQuery, String token) {
        Long id = jwtHelper.getUserId(token);
        User user = userMapper.selectById(id);
        if (user != null) {
            Page<SearchRecords> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
            LambdaQueryWrapper<SearchRecords> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SearchRecords::getUserId, user.getId());
            IPage<SearchRecords> iPage = this.page(page, queryWrapper);
            IPage<SearchRecordVo> recordVoIPage = iPage.convert(item -> {
                SearchRecordVo vo = new SearchRecordVo();
                BeanUtils.copyProperties(item, vo);
                return vo;
            });
            return PageResult.success(recordVoIPage);
        }
        return PageResult.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public PageResult<SearchArticleVo> getSearchArticle(BasePageQuery basePageQuery, String searchKey,String token){
        if(!StringUtils.isEmpty(searchKey)){
            Long id = jwtHelper.getUserId(token);
            User user = userMapper.selectById(id);
            if(user != null){
                new Thread(()->{
                    LambdaQueryWrapper<SearchRecords> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(SearchRecords::getUserId,id)
                            .eq(SearchRecords::getSearchQuery,searchKey);
                    SearchRecords record = searchRecordsMapper.selectOne(queryWrapper);
                    if(record != null){
                        searchRecordsMapper.deleteById(record);
                    }
                    SearchRecords searchRecords = new SearchRecords();
                    searchRecords.setUserId(user.getId());
                    searchRecords.setSearchQuery(searchKey);
                    this.save(searchRecords);
                }).start();
            }
            Page<SearchArticleVo> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
            IPage<SearchArticleVo> iPage = searchRecordsMapper.searchArticleByKey(page,searchKey);
            return PageResult.success(iPage);
        }
        return PageResult.build(null,500,"搜索关键词不能为空！");
    }

}




